using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace atirado
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D imagem, imagem2, textura4;
        Texture2D miratank;
        Vector2 posicao;
        Player player;
        Texture2D texTiro;
        tiro2 tiro2;
        Texture2D mouse;
        Texture2D texturaSangue;
        SpriteFont fonte;

        Random randon = new Random();
        int tempo;

        List<Tiro> tiros;
        List<Inimigo> inimigos = new List<Inimigo>();
        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {
            imagem2 = Content.Load<Texture2D>("sangue");
            mouse = Content.Load<Texture2D>("mouse");
            spriteBatch = new SpriteBatch(GraphicsDevice);
            miratank = Content.Load<Texture2D>("mira2");
            imagem = Content.Load<Texture2D>("str");
            textura4 = Content.Load<Texture2D>("tank2");
            texturaSangue =Content.Load<Texture2D>("Sangue");
            fonte = Content.Load<SpriteFont>("SpriteFont1");

            Inimigo.Initialize(Content.Load<Texture2D>("Gryphon"), texturaSangue);
            inimigos.Add(new Inimigo(posicao));
            player = new Player(Content.Load<Texture2D>("tiro"),Content.Load<Texture2D>("tiro22"), Content.Load<Texture2D>("tank2"),miratank,mouse, new Vector2(500, 500), spriteBatch);
            
        }
        protected override void Update(GameTime gameTime)
        {
            tiros = player.Update(Keyboard.GetState(),Mouse.GetState() ,gameTime, player.matanca);
            tempo += gameTime.ElapsedGameTime.Milliseconds;
            if(tempo >= 1000)
            {
                inimigos.Add(new Inimigo(new Vector2(randon.Next(-50, -10), randon.Next(-50, 700))));
                tempo = 0;
            }

            foreach (Inimigo i in inimigos)
            {
                i.Update(gameTime, player.posicaoplayer,player.matanca);
            }

            for (int i = 0; i < tiros.Count; i++)
            {
                for (int j = 0; j < inimigos.Count; j++)
                {
                    if(tiros[i].colisao().Intersects(inimigos[j].colisao()))
                    {
                        if  (!inimigos[j].estamorto()) 
                        {
                            tiros.RemoveAt(i);
                            inimigos[j].sangue();
                            player.matanca++;
                            break;
                        }                        
                    }           
                }     
            }

            for (int s = 0; s < inimigos.Count; s++)
            {
                if (inimigos[s].colisao().Intersects(player.colisao())) 
                {
                    if (!inimigos[s].estamorto())
                    {
                        inimigos[s].sangue();
                        player.vidas--;
                    }
                }
            }

            base.Update(gameTime);

            
            
        }
        
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteSortMode.FrontToBack,BlendState.AlphaBlend);
            spriteBatch.Draw(imagem, posicao, Color.White);

            foreach (Inimigo i in inimigos)
            {
                i.Draw(gameTime, spriteBatch);
            }
            player.Draw(gameTime);

            spriteBatch.DrawString(fonte, player.vidas.ToString(), new Vector2(50, 50), Color.Yellow, 0, Vector2.Zero, 1, SpriteEffects.None,01);
            spriteBatch.DrawString(fonte, player.matanca.ToString(), new Vector2(450, 0), Color.SpringGreen, 0, Vector2.Zero, 1, SpriteEffects.None, 01);

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
